package com.sayyi.validate.valid;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 存储用于验证的map信息
 * @author xuchuang
 * date 2021/12/2 5:08 下午
 */
public final class ValidateMapCache {

    private ValidateMapCache() throws IllegalAccessException {
        throw new IllegalAccessException("禁止实例化");
    }

    /** 缓存 */
    private static final Map<String, Map<Object, Object>> CACHE = new ConcurrentHashMap<>(100);

    /**
     * 设置缓存
     * @param key
     * @param value
     */
    static void set(String key, Map<Object, Object> value) {
        CACHE.put(genKey(key), value);
    }

    /**
     * 获取缓存，全局可见
     * @param key
     * @return
     */
    public static Map<Object, Object> get(String key) {
        return CACHE.get(genKey(key));
    }

    /**
     * 获取并移除缓存
     * @param key
     * @return
     */
    static Map<Object, Object> remove(String key) {
        return CACHE.remove(genKey(key));
    }

    /**
     * 生成携带线程前缀的key
     * @param key
     * @return
     */
    private static String genKey(String key) {
        return Thread.currentThread().getName() + "-" + key;
    }

}
